package com.leon.alishop.core.util;

public class AliShopStringUtils {

	/**
	 * 获取字符个数 1个中文=2个英文
	 * 
	 * @param input
	 * @return
	 */
	public static int getCharLength(String input) {
		int len = input.length();
		if (len == 0)
			return len;
		int clen = 0, start = 0;
		do {
			if (isCJK(input.charAt(start))) {
				clen += 2;
			} else {
				clen++;
			}
		} while (++start < len);
		return clen;
	}

	/**
	 * 判断是否是中文，日文，韩文
	 * 
	 * @param c
	 * @return
	 */
	public static boolean isCJK(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
				// 韩文
				|| ub == Character.UnicodeBlock.HANGUL_SYLLABLES
				|| ub == Character.UnicodeBlock.HANGUL_JAMO
				|| ub == Character.UnicodeBlock.HANGUL_COMPATIBILITY_JAMO
				// 日文
				|| ub == Character.UnicodeBlock.HIRAGANA
				|| ub == Character.UnicodeBlock.KATAKANA
				|| ub == Character.UnicodeBlock.KATAKANA_PHONETIC_EXTENSIONS)
			return true;
		return false;
	}

	/**
	 * 秒转化为时间
	 * 
	 * @param second
	 * @return
	 */
	public static String sec2Time(long millisecond) {
		StringBuffer sb = new StringBuffer();
		int bes = 0;
		int second = (int) (millisecond / 1000l);
		if ((bes = (second / 3600)) > 0) {
			sb.append(bes).append("小时");
		}
		second = second % 3600;
		if ((bes = (second / 60)) > 0) {
			sb.append(bes).append("分");
		}
		second = second % 60;
		if (second > 0) {
			sb.append(second).append("秒");
		}
		return sb.toString();
	}

	/**
	 * 将HTML &#... 转为汉字
	 * @param inputs
	 * @return
	 */
	public static String specCToUnicode(String inputs) {
		if(null == inputs)
			return null;
		StringBuffer result = new StringBuffer();
		int position = inputs.indexOf("&#");
		int position2 = inputs.indexOf(";", position + 2);
		if (position >= 0 && position2 >= 0) {
			String befores = inputs.substring(0, position);
			String afters = inputs.substring(position2 + 1, inputs.length());
			String middles = inputs.substring(position + 2, position2);

			String hexString = Integer.toHexString(Integer.parseInt(middles));
			if (hexString.length() % 2 != 0) {
				hexString = "0".concat(hexString);
			}

			int hl = hexString.length() / 2;
			byte[] p = { -2, -1, 0, 0 };
			hl = 3;
			int initf = 0;
			for (int i = 0; i < hexString.length(); i += 2) {
				p[hl - 1] = (byte) Integer.parseInt(
						hexString.substring(i, i + 2), 16);
				// System.out.print (p[hl-1]);
				initf++;
				hl++;
			}
			try {
				result = result.append(befores).append(new String(p, "UTF-16"))
						.append(specCToUnicode(afters));
				// System.out.print(new String(p,"UTF-16")+"\n");
			} catch (Exception e) {
			}
		} else {
			result = result.append(inputs);
		}
		return result.toString();
	}
}
